


plot_fun_means = function(df, group_var, outcome, group_name, split){
  
  buffer = ":                              "
  group_name_buf <- paste0(group_name, buffer)
  group_var <- enquo(group_var)
  outcome <- enquo(outcome)
  
  df <- df %>% mutate(category = !!group_var)
  #levels_tmp_org = levels(df$category)
  
  if(split == TRUE){
    tmp =
      df %>% 
      filter(!is.na(!!outcome)) %>%
      filter(!is.na(!!group_var)) %>%
      mutate(outcome_reg = !!outcome) %>%
      group_by(category = !!group_var) %>% 
      filter(validated_election_outcome == "Elected") %>%
      do(tidy(lm_robust(outcome_reg ~ pre_post, data = ., clusters = ID, fixed_effects = ID))) %>%
      filter(term == "pre_post") %>%
      select(category, estimate, std.error)
    
    
    tmp =
      tmp %>%
      ungroup() %>%
      add_row(., category = group_name, estimate = 10, std.error = 0) %>%
      mutate(category = factor(category, levels = append(group_name, levels_tmp_org))) %>% 
      arrange(category)
    
  }
  
  if(split == FALSE){
    tmp_df =
      df %>% 
      filter(!is.na(!!outcome)) %>%
      filter(!is.na(!!group_var)) %>%
      mutate(outcome_reg = !!outcome) %>%
      filter(validated_election_outcome != "Elected")
    
    tmp =
      feols(outcome_reg ~ pre_post*category | ID, data = tmp_df, cluster = "ID") %>%
      tidy() %>%
      filter(str_detect(term, "\\:")) %>%
      select(estimate, std.error) %>%
      mutate(category = group_name)
    
    
  }
  
  
  
  return(tmp)
}




estimation_data <-
  estimation_data %>%
  mutate(islam_recode_plot = factor(islam_recode_plot, levels = c("Other religion", "Islam")),
         islam_party_recode_plot = factor(islam_party_recode_plot, levels = c("Not Islamist", "Islamist")),
         neurot_recode_plot = factor(neurot_recode_plot, levels = c("Low neuroticism", "High neuroticism")),
         incumbent_flag = factor(incumbent_flag, levels = c("Not Incumbent", "Incumbent")),
         gender_recode_plot = factor(gender_recode_plot, levels = c("Woman", "Man")),
         campaign_prob_success = factor(campaign_prob_success, levels = c("Low chance", "High chance")),
         javanese_recode_plot = factor(javanese_recode_plot, levels = c("Other ethnicity", "Javanese")),
         campaign_expend = factor(campaign_expend, levels = c("Low expenditure", "High expenditure")),
         age_recode_plot2 = case_when(V_2 > 50 ~ "> 50",
                                      V_2 <= 50 ~ "< 50"),
         age_recode_plot2 = factor(age_recode_plot2, levels = c("< 50", "> 50")))


gender_df_fail = plot_fun_means(df = estimation_data, group_var = gender_recode_plot, outcome = avg_score_trust_elect, 
                                group_name = "Gender: Male", split = FALSE) %>% mutate(type = "Trust in Elections", split = "Demographics")

age_df_fail = plot_fun_means(df = estimation_data, group_var = age_recode_plot2, outcome = avg_score_trust_elect, 
                             group_name = "Age: > 50", split = FALSE) %>% mutate(type = "Trust in Elections", split = "Demographics")

relg_df_fail = plot_fun_means(df = estimation_data, group_var = islam_recode_plot, outcome = avg_score_trust_elect, 
                              group_name = "Religion: Islam", split = FALSE) %>% mutate(type = "Trust in Elections", split = "Demographics")

eth_df_fail = plot_fun_means(df = estimation_data, group_var = javanese_recode_plot, outcome = avg_score_trust_elect, 
                             group_name = "Ethnicity: Javanese", split = FALSE) %>% mutate(type = "Trust in Elections", split = "Demographics")


expend_df_fail = plot_fun_means(df = estimation_data, group_var = campaign_expend, outcome = avg_score_trust_elect, 
                                group_name = "Campaign Expenditures: High", split = FALSE) %>% mutate(type = "Trust in Elections", split = "Politics")


prob_success_df_fail = plot_fun_means(df = estimation_data, group_var = campaign_prob_success, outcome = avg_score_trust_elect, 
                                      group_name = "Expectation of Success", split = FALSE) %>% mutate(type = "Trust in Elections", split = "Psychology")


islamist_df_fail = plot_fun_means(df = estimation_data, group_var = islam_party_recode_plot, outcome = avg_score_trust_elect, 
                                  group_name = "Party Type: Islamist", split = FALSE) %>% mutate(type = "Trust in Elections", split = "Politics")

pdip_df_fail = plot_fun_means(df = estimation_data, group_var = pdip_recode_plot, outcome = avg_score_trust_elect, 
                              group_name = "Party: PDIP", split = FALSE) %>% mutate(type = "Trust in Elections", split = "Politics")

incumbent_df_fail = plot_fun_means(df = estimation_data, group_var = incumbent_flag, outcome = avg_score_trust_elect, 
                                   group_name = "Incumbent", split = FALSE) %>% mutate(type = "Trust in Elections", split = "Politics")

rwa_df_fail = plot_fun_means(df = estimation_data, group_var = rwa_recode_plot, outcome = avg_score_trust_elect, 
                             group_name = "RWA Index: High", split = FALSE) %>% mutate(type = "Trust in Elections", split = "Psychology")

neurot_df_fail = plot_fun_means(df = estimation_data, group_var = neurot_recode_plot, outcome = avg_score_trust_elect, 
                                group_name = "Neuroticism: High", split = FALSE) %>% mutate(type = "Trust in Elections", split = "Psychology")



#do it for second outcome
estimation_data$avg_score_democ_support
gender_df_fail_2 = plot_fun_means(df = estimation_data, group_var = gender_recode_plot, outcome = avg_score_democ_support, 
                                  group_name = "Gender: Male", split = FALSE) %>% mutate(type = "Support for Democracy", split = "Demographics")

age_df_fail_2 = plot_fun_means(df = estimation_data, group_var = age_recode_plot2, outcome = avg_score_democ_support, 
                               group_name = "Age: > 50", split = FALSE) %>% mutate(type = "Support for Democracy", split = "Demographics")

relg_df_fail_2 = plot_fun_means(df = estimation_data, group_var = islam_recode_plot, outcome = avg_score_democ_support, 
                                group_name = "Religion: Islam", split = FALSE) %>% mutate(type = "Support for Democracy", split = "Demographics")

eth_df_fail_2 = plot_fun_means(df = estimation_data, group_var = javanese_recode_plot, outcome = avg_score_democ_support, 
                               group_name = "Ethnicity: Javanese", split = FALSE) %>% mutate(type = "Support for Democracy", split = "Demographics")


expend_df_fail_2 = plot_fun_means(df = estimation_data, group_var = campaign_expend, outcome = avg_score_democ_support, 
                                  group_name = "Campaign Expenditures: High", split = FALSE) %>% mutate(type = "Support for Democracy", split = "Politics")


prob_success_df_fail_2 = plot_fun_means(df = estimation_data, group_var = campaign_prob_success, outcome = avg_score_democ_support, 
                                        group_name = "Expectation of Success", split = FALSE) %>% mutate(type = "Support for Democracy", split = "Psychology")


islamist_df_fail_2 = plot_fun_means(df = estimation_data, group_var = islam_party_recode_plot, outcome = avg_score_democ_support, 
                                    group_name = "Party Type: Islamist", split = FALSE) %>% mutate(type = "Support for Democracy", split = "Politics")

pdip_df_fail_2 = plot_fun_means(df = estimation_data, group_var = pdip_recode_plot, outcome = avg_score_democ_support, 
                                group_name = "Party: PDIP", split = FALSE) %>% mutate(type = "Support for Democracy", split = "Politics")

incumbent_df_fail_2 = plot_fun_means(df = estimation_data, group_var = incumbent_flag, outcome = avg_score_democ_support, 
                                     group_name = "Incumbent", split = FALSE) %>% mutate(type = "Support for Democracy", split = "Politics")

rwa_df_fail_2 = plot_fun_means(df = estimation_data, group_var = rwa_recode_plot, outcome = avg_score_democ_support, 
                               group_name = "RWA Index: High", split = FALSE) %>% mutate(type = "Support for Democracy", split = "Psychology")

neurot_df_fail_2 = plot_fun_means(df = estimation_data, group_var = neurot_recode_plot, outcome = avg_score_democ_support, 
                                  group_name = "Neuroticism: High", split = FALSE) %>% mutate(type = "Support for Democracy", split = "Psychology")











plot <-
  rbind(relg_df_fail, gender_df_fail) %>%
  rbind(., age_df_fail) %>%
  rbind(., eth_df_fail) %>%
  rbind(., expend_df_fail) %>%
  rbind(., prob_success_df_fail) %>%
  rbind(., islamist_df_fail) %>%
  rbind(., pdip_df_fail) %>%
  rbind(., incumbent_df_fail) %>%
  rbind(., rwa_df_fail) %>%
  rbind(., neurot_df_fail) %>%
  
  rbind(., relg_df_fail_2) %>%
  rbind(., gender_df_fail_2) %>%
  rbind(., age_df_fail_2) %>%
  rbind(., eth_df_fail_2) %>%
  rbind(., expend_df_fail_2) %>%
  rbind(., prob_success_df_fail_2) %>%
  rbind(., islamist_df_fail_2) %>%
  rbind(., pdip_df_fail_2) %>%
  rbind(., incumbent_df_fail_2) %>%
  rbind(., rwa_df_fail_2) %>%
  rbind(., neurot_df_fail_2) %>%
  
  ggplot(aes(x=estimate, y = category)) +
  #scale_shape_manual(values=1:7) +
  geom_point(fill = "black") +
  geom_errorbarh(aes(xmin = (estimate - 1.67*std.error), xmax = (estimate + 1.67*std.error), height = 0)) +
  geom_errorbarh(aes(xmin = (estimate - 1.96*std.error), xmax = (estimate + 1.96*std.error), height = 0), linewidth = 0.25) +
  theme_bw() + 
  #coord_cartesian(xlim = c(-0.35, 0.15)) +
  ylab("") + 
  # theme(text=element_text(size=14, family="Times")) + 
  theme(panel.grid.minor = element_blank(), panel.grid.major.x = element_blank(), panel.grid.major.y = element_line(size = .2)) + 
  theme(legend.title = element_blank()) + 
  theme(legend.position = "none") +
  theme(legend.text=element_blank()) +
  theme(strip.background =element_rect(fill="white")) +
  #geom_vline(xintercept = -0.2080301-1.96*0.05696058, color = "black", linetype = "dashed") +
  #geom_vline(xintercept = -0.2080301+1.96*0.05696058, color = "black", linetype = "dashed") +
  xlab("Effect of Electoral Defeat on Grievance") +
  geom_vline(xintercept = 0, linetype = "dashed") +
  #geom_rect(aes(xmin=-0.2080301-1.96*0.05696058, xmax = -0.2080301+1.96*0.05696058, ymax = 36, ymin = 0), fill = "lightgrey", alpha = 0.01) +
  #geom_rect(aes(xmin=-10, xmax = 10, ymax = 37.5, ymin = 36.5), fill = "red", alpha = 0.01) +
  #geom_hline(yintercept = 36.5, color = "red", linetype = "solid") +
  #geom_hline(yintercept = 37.5, color = "red", linetype = "solid") +
  facet_grid(split ~ fct_rev(type), scales = "free_y", space = 'free') +
  theme(strip.text.y = element_text(angle = 90)) +
  scale_color_manual(values = c("darkgrey", "black"))


ggsave(plot = plot, "./outputs/figures/figure_2.pdf", width = 8, height = 4)
ggsave(plot = plot, "./outputs/figures/figure_2.eps", device = "eps", width = 8, height = 4)



